﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Globalization;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;

namespace GabageFactory.Models
{
    public class Reposity
    {
        protected GbFactoryDataContext _dataContext = new GbFactoryDataContext();

        public void SubmitChanges()
        {
            _dataContext.SubmitChanges();
        }

        public List<T> Pagination<T>(IEnumerable<T> records, Pagination pagination)
        {
            List<T> result = new List<T>();
            if (records != null)
            {
                pagination.TotalPage = (int)Math.Ceiling(records.Count() * 1.0 / 15);
                pagination.PageNo = pagination.PageNo <= 0 ? 1 : (pagination.PageNo > pagination.TotalPage ? pagination.TotalPage : pagination.PageNo);
                pagination.TotalPage = pagination.TotalPage > 0 ? pagination.TotalPage : 1;

                int indexBegin = (pagination.PageNo - 1) * 15;
                int count = pagination.PageNo < pagination.TotalPage ? 15 : (records.Count() - indexBegin);
                result = records.ToList().GetRange(indexBegin, count);
            }

            if (pagination.PageNo > pagination.TotalPage)
            {
                pagination.PageNo = pagination.TotalPage;
            }

            if (pagination.PageNo <= 0)
            {
                pagination.PageNo = 1;
            }

            return result;
        }
    }
}